package com.hbyj.struts.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.YjdcYjdc01;
import com.hbyj.hbm.vo.YjdcYjdc06;


public class YjdcdXxdrAction {
	
	private File txtwenben;
	private File quanmao;
	private File jubu1;
	private File jubu2;
	
	public File getTxtwenben() {
		return txtwenben;
	}

	public void setTxtwenben(File txtwenben) {
		this.txtwenben = txtwenben;
	}

	public File getQuanmao() {
		return quanmao;
	}

	public void setQuanmao(File quanmao) {
		this.quanmao = quanmao;
	}

	public File getJubu1() {
		return jubu1;
	}

	public void setJubu1(File jubu1) {
		this.jubu1 = jubu1;
	}

	public File getJubu2() {
		return jubu2;
	}

	public void setJubu2(File jubu2) {
		this.jubu2 = jubu2;
	}

	
	public void yjdcdxxdrToDB() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String tybhAndTime = request.getParameter("tybhAndTime");
		boolean flag = true;
		if(quanmao!=null){
			if(uploadFile("uploadfolder/uploadImgForYJDC3G",tybhAndTime+"_1.jpg",quanmao)){
			}else{
				flag = false;
			}
		}
		if(jubu1!=null){
			if(uploadFile("uploadfolder/uploadImgForYJDC3G",tybhAndTime+"_2.jpg",jubu1)){
			}else{
				flag = false;
			}
		}
		if(jubu2!=null){
			if(uploadFile("uploadfolder/uploadImgForYJDC3G",tybhAndTime+"_3.jpg",jubu2)){
			}else{
				flag = false;
			}
		}
		//开始读取文本的数据
		FileReader in = new FileReader(txtwenben);
		char buf[] = new char[1024];
		int len = in.read(buf);
		in.close();
		String str = new String(buf,0,len);
		JSONObject obj = JSONObject.fromObject(str);
		YjdcYjdc01 yjdc01 = (YjdcYjdc01)JSONObject.toBean(obj,YjdcYjdc01.class);
		yjdc01.setId(null);
		String leixing = yjdc01.getYjdc01340();//在文本中保存的是类型
		yjdc01.setYjdc01340("");
		YjdcYjdc06 yjdc06 = new YjdcYjdc06();
		yjdc06.setYjdc06000(yjdc01.getYjdc01000());
		yjdc06.setYjdc06010(leixing);
		yjdc06.setYjdc06080(yjdc01.getYjdc01270());
		if(DbTools.saveObj(yjdc01)&&DbTools.saveObj(yjdc06)){
			ServletActionContext.getResponse().getOutputStream().write("true".getBytes("utf-8"));
		}else{
			ServletActionContext.getResponse().getOutputStream().write("false".getBytes("utf-8"));
		}
	}
	
	//将上传的文件放到指定的目录下面
	public boolean uploadFile(String serverPath,String newFileName,File f){
		String getFilePath = ServletActionContext.getServletContext().getRealPath("/"+serverPath);
		if(copyFile(getFilePath, newFileName ,f)){
			return true;
		}else{
			return false;
		}
	}
	
	private boolean copyFile(String uploadpath, String uploadFileFileName, File f) {
			String getfilePath = uploadpath;
	        String newfilename = uploadFileFileName;
	        //要存放的文件对象
	        File uploadFile  = new File(getfilePath+"/"+newfilename);
	        // 复制file对象，实现上传 
	        try {
				FileUtils.copyFile(f,uploadFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return true;
	}
	
}
